package sample;
import org.dom4j.*;
import org.dom4j.io.*;

import java.io.*;
import java.util.*;

/**
 * @author
 * @date 2018/05/24/22:36
 * @description javaAPI第三章
 */
public class Test1{
    static  List<Element> productList=null;
    static List<Product> products=new ArrayList();
    static  Element rootEle=null;
    static  Document doc=null;

    /**
     * 商品信息显示
     */
    public static void showProductInfo(){
        for (Product product : products) {
            System.out.println(product);
        }
    }

    /**
     * 修改产品信息
     * @param number
     */
    public  static  void  modfiyProduct(String number){
        boolean con=false;
        Scanner input=new Scanner(System.in);
        for (int i = 0; i <productList.size() ; i++) {
            if(productList.get(i).attributeValue("no").equals(number)){
                System.out.println("请输入库存量:");
                Integer num=input.nextInt();
                productList.get(i).element("stockcount").setText(num.toString());
                XMLWriter writer= null;
                try {
                    FileOutputStream out =new FileOutputStream("第六章/data/products.xml");
                    // 指定文本的写出的格式：
                    OutputFormat format=OutputFormat.createPrettyPrint();   //漂亮格式：有空格换行
                    format.setEncoding("UTF-8");
                    //1.创建写出对象
                    writer = new XMLWriter(out,format);
                    //2.写出Document对象
                    writer.write(doc);
                    con=true;
                    System.out.println("修改成功!");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                catch (IOException e){
                    e.printStackTrace();
                }
                finally {
                    if(null!=writer){
                        try {
                            writer.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        if(!con){
            System.out.println("没有找到商品号");
        }
    }


    /**
     * 查询产品信息
     * @param number
     */
    public static  void searchProduct(String number){
        boolean con=false;
        for (Product product : products) {
            if(number.equals(product.getProductno())){
                con=true;
                System.out.println("商品编号\t  生产厂家\t 商品名称\t商品价格\t  计价单位\t折扣\t  库存量");
                System.out.println(product);
            }
        }
        if(!con){
            System.out.println("查询不到此产品");
        }
    }
    public  static  double  buyProduct(String number) {
        boolean con = false;
        double totalprice = 0;
        Scanner input = new Scanner(System.in);
        for (Element product : productList) {
            if (product.attributeValue("no").equals(number)) {
                con=true;
                System.out.println("请输入购买数量:");
                Integer quantity = input.nextInt();
                if (product.elementTextTrim("stockcount").compareTo(quantity.toString()) >= 0) {
                    Integer stock = Integer.parseInt(product.elementTextTrim("stockcount"));
                    product.element("stockcount").setText(Integer.toString(stock - quantity));
                    if (product.elementTextTrim("discount").equals("折扣")) {
                        totalprice = quantity * Integer.parseInt(product.elementTextTrim("price"))*0.7;

                    }else {
                        totalprice = quantity * Integer.parseInt(product.elementTextTrim("price"));
                    }
                    XMLWriter writer = null;
                    try {
                        FileOutputStream out = new FileOutputStream("data/Products.xml");
                        OutputFormat format = OutputFormat.createPrettyPrint();
                        format.setEncoding("UTF-8");
                        writer = new XMLWriter(out, format);
                        writer.write(doc);
                        System.out.println("购买成功!");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (null != writer) {
                            try {
                                writer.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            return totalprice;
                        }
                    }
                }else if(product.elementTextTrim("stockcount").compareTo(quantity.toString()) ==-1){
                    System.out.println("库存量不足");
                }
            }
        }
        if(!con){
            System.out.println("找不到此商品");
        }
        return  totalprice;
    }
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        SAXReader reader=new SAXReader();
        try {
            doc=reader.read(new File("第六章/data/products.xml"));
            rootEle=doc.getRootElement();
            productList=rootEle.elements("productno");
            for (Element element : productList) {
                String id=element.attributeValue("no");
                String name=element.elementTextTrim("name");
                String price=element.elementTextTrim("price");
                String discount=element.elementTextTrim("discount");
                String unit=element.elementTextTrim("unit");
                String stockcount=element.elementTextTrim("stockcount");
                String manufacturer=element.elementTextTrim("manufacturer");
                products.add(new Product(name,id,price,unit,Integer.parseInt(stockcount),discount,manufacturer));
            }

            System.out.println("1.显示产品信息:");
            System.out.println("2.查询产品信息");
            System.out.println("3.修改产品信息");
            System.out.println("4.购买商品:");
            System.out.print("请选择:");
            int chocie=input.nextInt();
            switch (chocie){
                case 1:
                    if(productList.size()!=0){
                        System.out.println("商品编号\t  生产厂家\t 商品名称\t商品价格\t  计价单位\t折扣\t  库存量");
                        showProductInfo();
                    }
                    else{
                        System.out.println("没有产品");
                    }
                    break;
                case 2:
                    System.out.print("请输入商品编号:");
                    String number=input.next();
                    searchProduct(number);
                    break;
                case 3:
                    System.out.println("请输入商品编号:");
                    number=input.next();
                    modfiyProduct(number);
                    break;
                case 4:
                    System.out.println("请输入商品编号:");
                    number=input.next();
                    System.out.println(buyProduct(number));
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}